<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../back-common.jsp"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
	<%
			Long offset = (Long)request.getAttribute("offset");
	%>	
	<form:form  action="${contextPath}/admin/member/user/query" id="form1" method="post">
        <table class="${tableclass}" style="width: 100%">
	    <thead>
	    	<tr>
	    	   		<th><strong class="am-text-primary am-text-lg">权限管理</strong> /  用户管理 </th>
	    	   </tr>
	    </thead>
	     <tbody><tr><td>
	      <div align="left" style="padding: 3px">
				    <input type="hidden" id="curPageNO" name="curPageNO" value="<%=request.getAttribute("curPageNO")%>">
							&nbsp; 用户名
							<input class="${inputclass}" type="text" name="name" maxlength="50" value="${bean.name }" />
								&nbsp;状态 
								<select id="enabled" name="enabled"  class="${selectclass}">
								  <ls:optionGroup type="select" required="false" cache="true"  beanName="ENABLED" selectedValue="${bean.enabled}"/>
					            </select>
							<input  class="${btnclass}" type="submit" value="搜索"/>
							<!--
							<input type="button" value="创建用户" onclick='window.location="<ls:url address='/admin/member/user/load'/>"'/>
							  -->
				 </div>
	      </td></tr></tbody>
	    </table>
	</form:form>
	 <div align="center">
        <%@ include file="/WEB-INF/pages/common/messages.jsp"%>
    <display:table name="list" requestURI="${contextPath}/admin/member/user/query"  id="item" export="false" class="${tableclass}" style="width:100%">
    	<display:column title='<input type="checkbox" id="checkedAll">' class="orderwidth">
    		<input type="checkbox" name="userId" value="${item.id}" can-offline="${item.enabled == 1}" <c:if test="${item.enabled != 1}">disabled=true</c:if>/>
    	</display:column>
      <display:column title="顺序"  class="orderwidth"><%=offset++%></display:column>
      <display:column title="用户名 " property="name" sortable="true"></display:column>
    <%--   <display:column title="用户手机号 " property="name" sortable="true"></display:column> --%>
      <display:column title="状态">
      <ls:optionGroup type="label" required="true" cache="true"
	                beanName="ENABLED" selectedValue="${item.enabled}" defaultDisp=""/>
      </display:column>
      <display:column title="备注" property="note"></display:column>
      <display:column title="用户角色">
      	<a href="<ls:url address='/admin/member/user/roles/${item.id}?appNo=FRONT_END'/>">用户角色</a>
      </display:column>
      <display:column title="用户权限">
      	<a href="<ls:url address='/admin/member/user/functions/${item.id}?appNo=FRONT_END'/>" >用户权限</a>
      </display:column>
      
	      <display:column title="修改密码">
	      	<a href="<ls:url address='/admin/member/user/update/${item.id}?appNo=FRONT_END'/>">修改密码</a> 
	      </display:column>
      
       <display:column title="状态" style="width:70px;">
      		<div class="am-btn-toolbar">
		      <div class="am-btn-group am-btn-group-xs">
			<c:choose>
			  		<c:when test="${item.enabled == 1}">
			  			<button class="am-btn am-btn-default am-btn-xs am-hide-sm-only" style="color:#f37b1d;" onclick="changeUserStatus('${item.id}', 0);"><span class="am-icon-arrow-down"></span>下线</button>
			  		</c:when>
			  		<c:otherwise>
			  			<button class="am-btn am-btn-default am-btn-xs am-hide-sm-only" style="color:#0e90d2;" onclick="changeUserStatus('${item.id}', 1);"><span class="am-icon-arrow-up"></span>上线</button>
			  		</c:otherwise>
			  	</c:choose>
			</div>
		 	</div>
      </display:column>
    </display:table>
    
    <div style="float:left;margin-left:10px;margin-bottom:25px;" >
	    <input type="button" value="批量下线" id="batch-offline">
	</div><br/>
	
        <c:if test="${not empty toolBar}">
            <c:out value="${toolBar}" escapeXml="${toolBar}"></c:out>
        </c:if>
    </div>
	<div align="center">
    
<script language="JavaScript" type="text/javascript">

$("#checkedAll").on("click",function(){
	if($(this).is(":checked")){
		$("input[type=checkbox][name=userId][can-offline=true]").prop("checked",true);
	}else{
		$("input[type=checkbox][name=userId][can-offline=true]").removeAttr("checked");
	}
});

$("#batch-offline").on("click",function(){
	var userIds=$("input[type=checkbox][name=userId][can-offline=true]:checked");
	var userIdList=new Array();
	userIds.each(function(){
		var userId=$(this).val();
		userIdList.push(userId);
	});
	var userIdStr=userIdList.toString();
	if(userIdStr==""){
		art.dialog.alert("请选择要下线的用户");
		return false;
	}
	
	$.ajax({
  		url : "${contextPath}/admin/system/userDetail/batchOfflineUser/" +userIdStr,
  		type : "PUT",
  		dataType :"JSON",
  		async : true,
  		error : function(xhr,status,error){
  			art.dialog.tips("网络错误,请稍后重试!");
  		},
  		success : function(result,status,xhr){
  			if(result == "OK"){
  				art.dialog.alert("批量下线用户成功。",function(){
  					window.location.reload(true);
  				});
  			}else{
  				art.dialog.alert(result);
  			}
  		}
  	});
	
});
/* 		  $(document).ready(function(){
          	 $("button[name='statusImg']").click(function(event){
	          		$this = $(this);
	          		initStatus($this.attr("itemId"), $this.attr("itemName"),  $this.attr("status"), "${contextPath}/admin/system/userDetail/updatestatus/", $this,"${contextPath}");
	       	 });
          }); */
          
          function changeUserStatus(userId, status){
          	if(status == 0){
          		art.dialog.confirm("您确定要下线该用户?<p style='margin-top:5px;font-size:13px;color:#333;'>PS:如果该用户有商城,那么用户的商城也会自动下线!</p>",function(){
          			sendReq(userId, status, "恭喜您,下线成功!");
          		});
          	}else{
          		art.dialog.confirm("您确定要上线该用户?",function(){
          			sendReq(userId, status, "恭喜您,上线成功!<p style='margin-top:5px;font-size:13px;color:#333;'>如果该用户有商城,需要去店铺管理手动上线商城,否则商城不会上线!</p>");
          		});
          	}
          }
          
          function sendReq(userId, status, msg){
	          	$.ajax({
	          		url : "${contextPath}/admin/system/userDetail/changeStatus/" + userId + "/" + status,
	          		type : "POST",
	          		dataType :"JSON",
	          		async : true,
	          		error : function(xhr,status,error){
	          			art.dialog.tips("网络错误,请稍后重试!");
	          		},
	          		success : function(result,status,xhr){
	          			if(result == "OK"){
	          				art.dialog.alert(msg,function(){
	          					window.location.reload(true);
	          				});
	          			}else{
	          				art.dialog.alert(result);
	          			}
	          		}
	          	});
          }
</script>
